package com.mysql.Action;
/*
* 控制层
* */

import com.mysql.DAO.StudentsDAO;
import com.mysql.model.Students;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class StudentsAction {
//    增
    public void add() throws SQLException {
        StudentsDAO sd = new StudentsDAO();
        Scanner sc = new Scanner(System.in);
        Students stu = new Students();
        System.out.print("请输入学生姓名：");
        stu.setName(sc.next());
        System.out.print("请输入学生年龄：");
        stu.setAge(sc.nextInt());
        System.out.print("请输入学生电话号码：");
        stu.setPhone(sc.next());
        sd.addStudents(stu);
        System.out.println("学生信息添加成功！");
    }

//    删
    public void del() throws SQLException {
        StudentsDAO sd = new StudentsDAO();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除的学生姓名：");
        String name = sc.next();
        sd.delStudents(name);
    }

//    查
    public void query() throws SQLException {
        StudentsDAO sd = new StudentsDAO();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要查询的学生姓名：");
        String name = sc.next();
        Students stu = sd.selStudents(name);
        System.out.println("该生的学生信息是：");
        System.out.println("姓名：" + stu.getName());
        System.out.println("年龄" + stu.getAge());
        System.out.println("电话" + stu.getPhone());
    }

    public void queryall() throws SQLException {
        StudentsDAO sd = new StudentsDAO();
        List<Students> stuls = sd.getStudents();
        System.out.println("所有学生信息为：");
        for (Students stu : stuls) {
            System.out.println("姓名" + stu.getName() + "  年龄" + stu.getAge() + "  电话" + stu.getPhone());
        }
    }

//    改
    public void update() throws SQLException {
        StudentsDAO sd = new StudentsDAO();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要修改的学生姓名：");
        String name = sc.next();
        Students stu = sd.selStudents(name);
        do {
            System.out.println("请选择要修改的学生信息:");
            System.out.println("1.姓名");
            System.out.println("2.年龄");
            System.out.println("3.电话");
            System.out.println("4.结束修改");
            switch (sc.next()) {
                case "1":
                    System.out.print("请输入要修改的姓名:");
                    stu.setName(sc.next());
                    System.out.println("姓名修改成功!");
                    break;
                case "2":
                    System.out.print("请输入要修改的年龄:");
                    stu.setName(sc.next());
                    System.out.println("年龄修改成功!");
                    break;
                case "3":
                    System.out.print("请输入要修改的电话:");
                    stu.setName(sc.next());
                    System.out.println("电话修改成功!");
                    break;
                case "4":
                    sd.updateStudents(stu);
                    return;
                default:
                    System.out.println("请重新选择!");
            }
            System.out.println("请按任意键继续!");
            sc.next();
        }while(true);
    }
}
